package com.zbkj.admin.controller.platform;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.model.Vehicle;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.VehicleRequest;
import com.zbkj.common.request.VehicleSearchRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.VehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 车辆表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/vehicle")
@Api(tags = "车辆表")
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    /**
     * 分页显示车辆表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author keNan
     * @since 2025-06-25
     */
    @PreAuthorize("hasAuthority('platform:vehicle:list')")
    @ApiOperation(value = "车辆分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Vehicle>> getList(@Validated VehicleSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<Vehicle> vehicleCommonPage = CommonPage.restPage(vehicleService.getList(request, pageParamRequest));
        return CommonResult.success(vehicleCommonPage);
    }

    /**
     * 新增车辆表
     *
     * @param vehicleRequest 新增参数
     * @author keNan
     * @since 2025-06-25
     */
    @PreAuthorize("hasAuthority('platform:vehicle:save')")
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated VehicleRequest vehicleRequest) {
        if (vehicleService.saveVehicle(vehicleRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除车辆表
     *
     * @param id Integer
     * @author keNan
     * @since 2025-06-25
     */
    @PreAuthorize("hasAuthority('platform:vehicle:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        LambdaUpdateWrapper<Vehicle> lqw = Wrappers.lambdaUpdate();
        lqw.set(Vehicle::getIsDel, 1);
        lqw.eq(Vehicle::getId, id);
        if (vehicleService.update(lqw)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改车辆表
     *
     * @param vehicleRequest 修改参数
     * @author keNan
     * @since 2025-06-25
     */
    @PreAuthorize("hasAuthority('platform:vehicle:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Valid VehicleRequest vehicleRequest) {
        if (null == vehicleRequest.getId()) {
            return CommonResult.failed("id不能为空");
        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleRequest, vehicle);
        vehicle.setId(vehicleRequest.getId());

        if (vehicleService.updateById(vehicle)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询车辆表信息
     *
     * @param id Integer
     * @author keNan
     * @since 2025-06-25
     */
    @PreAuthorize("hasAuthority('platform:vehicle:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<Vehicle> info(@RequestParam(value = "id") Integer id) {
        Vehicle vehicle = vehicleService.getById(id);
        return CommonResult.success(vehicle);
    }


}



